


import 'dart:convert';

import 'package:aqua_plunge/model/userinfo.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../util/HiCache.dart';
import '../util/MedalType.dart';

class AppManager{


  AppManager._();
  static AppManager? _instance;
  static AppManager? getInstance(){
    _instance ??= AppManager._();
    return _instance;
  }

  //通行令牌 token
  static const token = "token";
  static const valid = "valid";

  static UserInfo? accountInfo;

  ///用户信息
  static const userinfo = "userinfo";

  static getToken(){
    return HiCache.getInstance().get(token)??"";
  }

  static saveToken(String tokenStr){
    HiCache.getInstance().setString(token,tokenStr);
  }

  static setUserInfo(String jsonString){
    HiCache.getInstance().setString(userinfo, jsonString);
  }

  static UserInfo getUserInfo(){
    if(accountInfo == null){
      var jsonMap = json.decode(HiCache.getInstance().get(userinfo));
      accountInfo = UserInfo.fromJson(jsonMap as Map<String, dynamic>);
    }
    return accountInfo!;
  }

  static resetUserinfo(){
    accountInfo = null;
    saveToken('');
    resetAccount();
  }

  static isFirstLaunch(){
    var prefs = HiCache.getInstance();
    bool isFirstLaunch = (prefs.get('isFirstLaunch') ?? true);
    if (isFirstLaunch) {
      prefs.setBool('isFirstLaunch', false);
    }
    return isFirstLaunch;
  }


  static authorTimesPlus(){
    HiCache.getInstance().setInt('audio',getAuthorTimes() +1);
  }
  static getAuthorTimes(){
     return HiCache.getInstance().get('audio')??0;
  }

  static isMedalFirstLaunch(MedalType type){
    var prefs = HiCache.getInstance();
    if(isAcquireMedal(type) == false) return;
    bool isFirstLaunch = (prefs.get(type.toString())==null? true:prefs.get(type.toString()) as bool);
    if (isFirstLaunch) {
      prefs.setBool(type.toString(), false);
    }
    return isFirstLaunch;
  }

  static isAcquireMedal(MedalType type){
    switch(type){
      case MedalType.level1:return true;
      case MedalType.level2:
        return HiCache.getInstance().get('level2')??false;
      case MedalType.level3:
        return HiCache.getInstance().get('level3')??false;
      case MedalType.level4:
        return HiCache.getInstance().get('level4')??false;
      case MedalType.upgrade1:
        return HiCache.getInstance().get('upgrade1')??false;
      case MedalType.upgrade2:
        return HiCache.getInstance().get('upgrade2')??false;
      case MedalType.upgrade3:
        return HiCache.getInstance().get('upgrade3')??false;
      case MedalType.upgrade4:
        return HiCache.getInstance().get('upgrade4')??false;
    }
  }

  static getMedal(MedalType type){
    switch(type){
      case MedalType.level2:
        HiCache.getInstance().setBool('level2',true);
        break;
      case MedalType.level3:
        HiCache.getInstance().setBool('level3',true);
        break;
      case MedalType.level4:
        HiCache.getInstance().setBool('level4',true);
        break;
      case MedalType.upgrade1:
        HiCache.getInstance().setBool('upgrade1',true);
        break;
      case MedalType.upgrade2:
        HiCache.getInstance().setBool('upgrade2',true);
        break;
      case MedalType.upgrade3:
        HiCache.getInstance().setBool('upgrade3',true);
        break;
      case MedalType.upgrade4:
        HiCache.getInstance().setBool('upgrade4',true);
        break;
      default:break;
    }
  }


  static resetAccount(){

    HiCache.getInstance().setBool('level2', false);
    HiCache.getInstance().setBool('level3', false);
    HiCache.getInstance().setBool('level4', false);
    HiCache.getInstance().setBool('upgrade1', false);
    HiCache.getInstance().setBool('upgrade2', false);
    HiCache.getInstance().setBool('upgrade3', false);
    HiCache.getInstance().setBool('upgrade4', false);

  }


  static Future<void> saveBlockList(int userid) async {
    final prefs = await SharedPreferences.getInstance();
    var list = await getBlockList();
    if(list.isNotEmpty){
      if(!list.contains(userid)){
        list.add(userid);
      }
    }else{
      list.add(userid);
    }
    await prefs.setStringList('block', list.map((item) => item.toString()).toList());
  }

  static Future<void> removeBlockList(int userid) async {
    final prefs = await SharedPreferences.getInstance();
    var list = await getBlockList();
    if(list.isNotEmpty){
      if(list.contains(userid)){
        list.remove(userid);
      }
    }
    await prefs.setStringList('block', list.map((item) => item.toString()).toList());
  }

  // 读取 List<int>
  static Future<List<int>> getBlockList() async {
    final prefs = await SharedPreferences.getInstance();
    List<String>? stringList = prefs.getStringList('block');
    return stringList?.map((item) => int.parse(item)).toList() ?? [];
  }

  // 清空列表
  static clearList() {
    HiCache.getInstance().remove('block');
  }

  static showall(){
    return HiCache.getInstance().get('showall')??false;
  }

  static setshowallTrue(){
    HiCache.getInstance().setBool('showall',true);
  }


}